<?php


class User_model extends CI_Model{

	public function getFullName($user_id){
		$this->db->where("user_id",$user_id);
		$query = $this->db->get("user");
		$rs = $query->result();
		return @$rs[0]->user_fname." ".@$rs[0]->user_lname;
		 
	}
	
	
	public function auten_session($rs){
		$data=$rs[0];
		$newdata = array(
				'user_id'  => $data->user_id,
				'admin'     => $data->user_admin,
				'username'  => $data->user_username,
				'email'     => $data->user_email,
				'fullname'  => $data->user_fname." ".$data->user_lname,
			//	'position'     => $data->user_email,
			//	'department'  => $data->user_username,
			//	'factory'     => $data->user_email,
				'position'     => "Production",
				'department'  => "Engineering",
				'factory'     => "Khon Kaen",
				'mobile'  => $data->user_mobile,
				'login_time'     => date("Y-m-d H:i:s"),
				'logged_in' => TRUE
		);
		
		$this->session->set_userdata($newdata);
		
	}
	
	public function getUser(){
		$query = $this->db->get("user");
		return json_encode($query->result());
	}
	
	
	public function checkLogin($inData){
		 $this->db->where("user_username",$inData["username"]);
		 $this->db->where("user_password",md5($inData["password"]));
		 $query = $this->db->get("user");
		 if($query->num_rows() > 0){
		 	$rs = $query->result();
		 	$this->auten_session($rs);
		 	return 1;
		 }
		 else{
		 	return 0;
		 }
	}
 
}
?>
